package com.blue.base.web;

import com.aliyun.oss.OSSClient;
import com.blue.base.web.config.GlobalExceptionHandler;
import com.blue.base.web.util.AliYunOssUtil;
import com.blue.base.web.util.VerifyCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author liulei
 * @version 1.0
 */
@Configuration
@ConditionalOnProperty(value = "aliyun.oss")
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class WebAutoConfiguration {

    private static Logger log = LoggerFactory.getLogger(WebAutoConfiguration.class);

    /**
     * 如果使用配置spring.exception.strategy.customize:true则会注入该Bean
     */
    @Bean
    @ConditionalOnProperty(prefix = "spring.exception.strategy", name = "customize", havingValue = "true", matchIfMissing = true)
    public GlobalExceptionHandler defaultExceptionHandler() {
        log.info("初始化默认全局MVC下异常处理器....");
        return new GlobalExceptionHandler();
    }

    /**
     * 注入阿里云工具类
     */
    @Bean
    public AliYunOssUtil aliYunOssUtil(OSSClient ossClient) {
        return new AliYunOssUtil(ossClient);
    }

    /**
     * 注入图片验证码
     */
    @Bean
    public VerifyCodeUtil verifyCodeUtil(StringRedisTemplate stringRedisTemplate){
        return new VerifyCodeUtil(stringRedisTemplate);
    }
}
